package org.weasel.business;

import java.sql.Time;
import java.util.ArrayList;

import org.weasel.data.ProjectManager;

public class Project extends Item {
	private String name;
	private ArrayList<Developer> developers;
	private ArrayList<Release> releases;
	private Unscheduled unscheduled;
	private String path;
	private String id;
	private ProjectManager projMan;

	public Project(String id, String name, String path, ArrayList<Developer> allDevelopers) throws Exception{
		this.id = id;
		this.name = name;
		this.path = path;
		releases = new ArrayList<Release>();
		projMan = new ProjectManager(this);
		if(Utilities.existsFile(path)) {
//			releases = getReleases();
//			unscheduled = getUnscheduled();
			developers = new ArrayList<Developer>();
			for (Developer developer : allDevelopers) {
				for (String developerID : projMan.getDeveloperIDs()) {
					if (developer.getId().equals(developerID))
						developers.add(developer);
				}
			}
		} else {
			developers = new ArrayList<Developer>();
			releases = new ArrayList<Release>();
		}
	}
	
	public Project(String name, ArrayList<Developer> developers, ArrayList<Release> releases, String path) throws Exception {
		this.name = name;
		if(developers == null){
			this.developers = new ArrayList<Developer>();
		}else{
			this.developers = developers;
		}
		if(releases == null){
			this.releases = new ArrayList<Release>();
		}else{
			this.releases = releases;
		}
		//TODO what happens if path = null???
		if(path==null) {
			path = name + ".xml";
		}
		this.path = path;
		projMan = new ProjectManager(this);
	}

	/**
	 * This method searches a developer in the list of developers and returns it;
	 * otherwise it returns null
	 * @param developerID
	 * @return
	 */
	public Developer getDeveloper(String developerID) {
		for (Developer developer : developers) {
			if(developer.getId().equals(developerID)) {
				return developer;
			}
		}
		return null;
	}
	
	public void addDeveloper(Developer d) throws Exception {
		projMan.addDeveloper(d.getId());
		developers.add(d);
	}
	
	public void removeDeveloper(String developerID) throws Exception {
		for (int i = 0; i < developers.size(); i++) {
			Developer tmp = developers.get(i);
			if(tmp.getId().equals(developerID)){
				//remove from XML file too
				projMan.deleteDeveloper(developers.get(i).getId());
				developers.remove(i);
			}
		}
	}
	
	public Release getRelease(String releaseID){
		for (Release release : releases) {
			if(release.getId().equals(releaseID)){
				return release;
			}
		}
		return null;
	}
	
	public Release createAndAddRelease(String name) throws Exception{
		Release r = projMan.createRelease(name);
		releases.add(r);
		return r;
	}
	
	public void removeRelease(String releaseId) throws Exception{
		for (int i = 0; i < releases.size(); i++) {
			if(releases.get(i).getId().equals(releaseId)){
				//remove from XML also
				projMan.deleteRelease(releases.get(i).getId());
				releases.remove(i);
			}
		}
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<Release> getReleases() {
		return releases;
	}

	public void setReleases(ArrayList<Release> releases) {
		this.releases = releases;
	}

	public ArrayList<Developer> getDevelopers() {
		return developers;
	}
	
	public void setDevelopers(ArrayList<Developer> developers) {
		this.developers = developers;
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	public String getId() {
		return id;
	}

	public Unscheduled getUnscheduled() {
		return unscheduled;
	}

	public void setUnscheduled(Unscheduled unscheduled) {
		this.unscheduled = unscheduled;
	}
	
	
	public UserStory getUnscheduledStory(String storyID){
		return unscheduled.getUnscheduledUserStory(storyID);
	}
	
	@Deprecated
	public UserStory addUnscheduledStory(String name, int businessValue, Time estimatedTime, Time usedTime, int effort) throws Exception{
		UserStory story = projMan.createUserStory(this.unscheduled.getId(), name, businessValue, estimatedTime, usedTime, effort);
		unscheduled.getUserStories().add(story);
		return story;
	}
	
	@Deprecated
	public void removeUnscheduledStory(String storyID) throws Exception{
		projMan.deleteUserStory(storyID);
		unscheduled.getUserStories().remove(unscheduled.getUnscheduledUserStory(storyID));
	}

	public ProjectManager getProjectManager() {
		return projMan;
	}
}
